package com.interview.javabasic.Algorithm.Floyd;/*
@李子宁
Happy,happy everyday!
冲鸭！
*/

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.Arrays;

public class FloydAlgorithm {
    public static void main(String[] args) {
        //测试创建图
        char[] vertex = {'A','B','C','D','E','F','G'};
        //创建邻接矩阵
        int[][] metrix = new int[vertex.length][vertex.length];
        final int N = 65535;
         metrix[0] = new int[]{0,5,7,N,N,N,2};
         metrix[1] = new int[]{5,0,N,9,N,N,3};
         metrix[2] = new int[]{7,N,0,N,8,N,N};
         metrix[3] = new int[]{N,9,N,0,N,4,N};
         metrix[4] = new int[]{N,N,8,N,0,5,4};
         metrix[5] = new int[]{N,N,N,4,5,0,6};
         metrix[6] = new int[]{2,3,N,N,4,6,0};
         //
        Graph graph = new Graph(vertex.length,metrix, vertex);
        graph.show();

    }
}

class Graph{
    private char[] vertex;
    private int[][] dis;//保存，从各个顶点出发到各个顶点的距离，最后的接货，也保留在这个数组
    private int[][] pre;//动态变化的

    //构造器
    public Graph(int length,int[][] matrix,char[] vertex){
        this.vertex = vertex;
        this.dis = matrix;
        this.pre = new int[length][length];
        //对pre初始化，存放的是前驱节点的下标
        for (int i = 0; i < pre.length; i++) {
            Arrays.fill(pre[i],i);
        }
    }
    //显示pre数组和dis数组
    public void show(){
        char[] vertex = {'A','B','C','D','E','F','G'};
        for (int i = 0; i < dis.length; i++) {
            //先将pre输出
            for (int j = 0; j < dis.length; j++) {
                System.out.print(vertex[pre[i][j]]+" ");
            }

            //输出dis数组
            for (int j = 0; j < dis.length; j++) {
                System.out.print("（"+vertex[i]+"到"+vertex[j]+"的最短路径是："+dis[i][j]+"） ");
            }
            System.out.println();
            System.out.println();
        }
    }
    //弗洛伊德算法
    public void floyd(){
        int len = 0;//变量保存距离
        //从中间顶点遍历，k就是中间顶点的下标
        for (int k = 0; k < dis.length; k++) {
            //从i顶点开始出发
            for (int i = 0; i < dis.length; i++) {
                //到达j顶点
                for (int j = 0; j <dis.length; j++) {
                        dis[i][j] = len;//更新距离
                        pre[i][j] = pre[k][j];
                    }
                }
            }
        }
    }
